/* global definitions and variables */

/* Copyright (C) 2009-2011 David Zanetti
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */


/* 8 MHz clock */
#define F_CPU 8000000
//#define F_CPU 1000000

/* ticklen, so .. this should be F_CPU/1000, right? except it's not
 * for reasons I don't quite understand. oops */
#define TICKLEN 19998

/* speed of the console port. 9600 is pretty universal */
#define CONSOLE_BAUD 57600

/* MAC address of our NIC */
uint8_t eth_mac[6];
/* Current IP address - may be sourced from eeprom or DHCP */
uint8_t ip_addr[4];
/* Current IP mask, same deal */
uint8_t ip_mask[4]; /* we get it from DHCP in dotted-quad so store it that way */
/* Current Gateway, 0.0.0.0 means none */
uint8_t ip_gateway[4];
/* FIXME: add routing table? */

char hostname[16];
uint8_t hostname_len;

/* flag collections
 * these are divided into groups for different purposes
 * they are 8-bit to minimise time without interrupts */

volatile uint8_t flag_stat;
#define FLAG_STAT_FACRESET 0x1

volatile uint8_t flag_clock;
#define FLAG_CLOCK_FREE 0x1
#define FLAG_CLOCK_SYNC 0x2
#define FLAG_CLOCK_SLEW 0x4
#define FLAG_CLOCK_TICK 0x8

volatile uint8_t flag_gps;
#define FLAG_GPS_LOCK 0x1
#define FLAG_GPS_PACKET 0x2
#define FLAG_GPS_EVENT_LOCK 0x4
#define FLAG_GPS_EVENT_TIME 0x8
#define FLAG_GPS_PPS 0x10

volatile uint8_t flag_ether;
#define FLAG_ETHER_INT 0x1
#define FLAG_ETHER_RX 0x2
#define FLAG_ETHER_LINK 0x4

volatile uint8_t flag_ip;
#define FLAG_IP_CONFIG 0x1
#define FLAG_IP_DHCP 0x2
#define FLAG_IP_DHCP_TIMER 0x4

volatile uint8_t flag_serial;
#define FLAG_SERIAL_U0_RX 0x1
#define FLAG_SERIAL_U1_RX 0x2
#define FLAG_SERIAL_U0_CMD 0x4
#define FLAG_SERIAL_U1_CMD 0x8;

#define set_flag(x,y) ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { x |= y; }
#define clear_flag(x,y) ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { x &= ~(y); }

volatile struct {
                uint32_t gps_frame_good;
                uint32_t gps_frame_bad;
                uint32_t gps_frame_ignored;
                uint32_t uptime;
                uint32_t eth_pkt_tx;
                uint32_t eth_pkt_rx;
                uint32_t clock_resync;
} global_counters;

volatile uint8_t gps_sat_count;

/* some defines we use eleswhere */

/* make usart0 behave like a console, echoback and flag returns as commands */
#define USART0_ECHO
#define USART0_IS_CONSOLE

/* a macro to perform a nop */

#define _nop() __asm volatile ("nop\n\t")

/* Defaults which will end up in factory reset/upgrade */

#define EEPROM_SIG 0xa09f
#define IP_DEFAULT_1 0
#define IP_DEFAULT_2 0
#define IP_DEFAULT_3 0
#define IP_DEFAULT_4 0
#define HOSTNAME "pcb-1.3\0"
#define HOSTNAME_LEN 8
#define ETH_LINK_MODE E_LINK_AUTO

/* Return codes for various bits of status in the Ethernet+IP layers */

/* nothing to do */
#define E_NOTHING 0
/* a reply */
#define E_REPLY 1
/* a reply, but sent to broadcast (255.255.255.255/ff:ff:ff:ff:ff:ff) */
#define E_REPLY_BROAD 2
/* error, ICMP port unreach */
#define E_ICMP_PORTUNREACH 3
/* MAC address is resolved, used by ip/arp */
#define E_RESOLVED 4
/* MAC is not resolved, but will be, please don't destroy the slot */
#define E_NEEDS_ARP 5

/* used in various bits of network code to swap between host and network order */
#define swap16(x) ((x << 8) | (x >> 8))
/* swap 32-bits, used in DHCP rewew/rebind calcs */
#define swap32(x) (uint32_t) (((uint32_t) ((x & 0xff) << 24)) | ((uint32_t) ((x & 0xff00) << 8)) | ((uint32_t) (x & 0xff0000) >> 8) | ((uint32_t) ((x & 0xff000000) >> 24)))

